package com.drg.center.service.impl;

import com.drg.center.dao.*;
import com.drg.center.entity.*;
import com.drg.center.entity.param.BaseIdsDTO;
import com.drg.center.entity.param.SetMatchDTO;
import com.drg.center.entity.result.*;
import com.drg.center.service.SysSettingService;
import com.guaike.core.entity.result.Result;
import com.guaike.core.utils.ListUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class SysSettingServiceImpl implements SysSettingService {
    private final DictDiagnosisDAO dictDiagnosisDAO;
    private final DictDiagnosisHisDAO dictDiagnosisHisDAO;
    private final DictOperationDAO dictOperationDAO;
    private final DictDrgOperationDAO dictDrgOperationDAO;
    private final DictProjectDAO dictProjectDAO;

    @Override
    public Result<List<BaseDictSettingVO>> searchHospitalDiagnosis(String search) {
        List<DictDiagnosisHisPO> pos = dictDiagnosisHisDAO.searchHospitalDiagnosis(search);
        List<BaseDictSettingVO> vos = new ArrayList<>();
        pos.forEach(po -> {
            BaseDictSettingVO vo = new BaseDictSettingVO();
            vo.setCode(po.getHisDiagnosisCode());
            vo.setName(po.getHisDiagnosisName());
            vos.add(vo);
        });
        return Result.success().data(vos).build();
    }

    @Override
    public Result<List<BaseDictSettingVO>> searchMedicalDiagnosis(String search) {
        List<BaseDictSettingVO> vos = new ArrayList<>();
        List<DictDiagnosisHisPO> hisPOS = dictDiagnosisHisDAO.searchMatchDiagnosis();
        List<String> list = hisPOS.stream().map(DictDiagnosisHisPO::getDiagnosisCode).collect(Collectors.toList());
        List<DictDiagnosisPO> pos = dictDiagnosisDAO.searchNoMatchMedicalDiagnosis(search, list);
        pos.forEach(po -> {
            BaseDictSettingVO vo = new BaseDictSettingVO();
            vo.setCode(po.getDiagnosisCode());
            vo.setName(po.getDiagnosisName());
            vos.add(vo);
        });
        return Result.success().data(vos).build();
    }

    @Override
    public Result<List<BaseDictSettingMatchVO>> searchMatchDiagnosis(String search) {
        List<DictDiagnosisHisPO> pos = dictDiagnosisHisDAO.searchMatchDiagnosis(search);
        List<BaseDictSettingMatchVO> vos = new ArrayList<>();
        pos.forEach(po -> {
            BaseDictSettingMatchVO vo = new BaseDictSettingMatchVO();
            vo.setId(po.getId());
            vo.setCode(po.getHisDiagnosisCode());
            vo.setName(po.getHisDiagnosisName());
            vo.setMedicalCode(po.getDiagnosisCode());
            vo.setMedicalName(po.getDiagnosisName());
            vos.add(vo);
        });
        return Result.success().data(vos).build();
    }

    @Override
    public Result<Boolean> matchDiagnosis(SetMatchDTO param) {
        DictDiagnosisHisPO hisPO = dictDiagnosisHisDAO.findOne(DictDiagnosisHisPO::getHisDiagnosisCode, param.getInHospitalCode());
        DictDiagnosisPO diagnosisPO = dictDiagnosisDAO.findOne(DictDiagnosisPO::getDiagnosisCode, param.getMedicalCode());
        hisPO.setDiagnosisCode(diagnosisPO.getDiagnosisCode());
        hisPO.setDiagnosisName(diagnosisPO.getDiagnosisName());
        dictDiagnosisHisDAO.updateById(hisPO);
        return Result.success().data(Boolean.TRUE).build();
    }

    @Override
    public Result<Boolean> cancelMatchDiagnosis(BaseIdsDTO param) {
        dictDiagnosisHisDAO.cancelMatchDiagnosis(param.getIds());
        return Result.success().data(Boolean.TRUE).build();
    }

    @Override
    public Result<List<BaseDictSettingVO>> searchHospitalOperation(String search) {
        List<DictOperationPO> pos = dictOperationDAO.searchHospitalOperation(search);
        List<BaseDictSettingVO> vos = new ArrayList<>();
        pos.forEach(po -> {
            BaseDictSettingVO vo = new BaseDictSettingVO();
            vo.setCode(po.getOperationCode());
            vo.setName(po.getOperationName());
            vos.add(vo);
        });
        return Result.success().data(vos).build();
    }

    @Override
    public Result<List<BaseDictSettingVO>> searchMedicalOperation(String search) {
        List<DictOperationPO> operationPOS = dictOperationDAO.searchMatchOperation();
        List<String> list = operationPOS.stream().map(DictOperationPO::getMedicareOperationCode).collect(Collectors.toList());
        List<DictDrgOperationPO> pos = dictDrgOperationDAO.searchNoMatchMedicalOperation(search, list);
        List<BaseDictSettingVO> vos = new ArrayList<>();
        pos.forEach(po -> {
            BaseDictSettingVO vo = new BaseDictSettingVO();
            vo.setCode(po.getOperationCode());
            vo.setName(po.getOperationName());
            vos.add(vo);
        });
        return Result.success().data(vos).build();
    }

    @Override
    public Result<List<BaseDictSettingMatchVO>> searchMatchOperation(String search) {
        List<DictOperationPO> pos = dictOperationDAO.searchMatchOperation(search);
        List<BaseDictSettingMatchVO> vos = new ArrayList<>();
        pos.forEach(po -> {
            BaseDictSettingMatchVO vo = new BaseDictSettingMatchVO();
            vo.setId(po.getId());
            vo.setCode(po.getOperationCode());
            vo.setName(po.getOperationName());
            vo.setMedicalCode(po.getMedicareOperationCode());
            vo.setMedicalName(po.getMedicareOperationName());
            vos.add(vo);
        });
        return Result.success().data(vos).build();
    }

    @Override
    public Result<Boolean> matchOperation(SetMatchDTO param) {
        DictOperationPO hisPO = dictOperationDAO.findOne(DictOperationPO::getOperationCode, param.getInHospitalCode());
        DictDrgOperationPO operationPO = dictDrgOperationDAO.findOne(DictDrgOperationPO::getOperationCode, param.getMedicalCode());
        hisPO.setMedicareOperationCode(operationPO.getOperationCode());
        hisPO.setMedicareOperationName(operationPO.getOperationName());
        dictOperationDAO.updateById(hisPO);
        return Result.success().data(Boolean.TRUE).build();
    }

    @Override
    public Result<Boolean> cancelMatchOperation(BaseIdsDTO param) {
        dictOperationDAO.cancelMatchOperation(param.getIds());
        return Result.success().data(Boolean.TRUE).build();
    }

    @Override
    public Result<List<ProjectVO>> searchProject(String search, Boolean isMain, Integer projectType) {
        List<DictProjectPO> pos = dictProjectDAO.searchProject(search, isMain, projectType);
        List<ProjectVO> vos = ListUtil.copyList(pos, ProjectVO.class);
        return Result.success().data(vos).build();
    }

    @Override
    public Result<Boolean> setMainProject(BaseIdsDTO param) {
        dictProjectDAO.setMainProject(param.getIds());
        return Result.success().data(Boolean.TRUE).build();
    }

    @Override
    public Result<Boolean> cancelMainProject(BaseIdsDTO param) {
        dictProjectDAO.cancelMainProject(param.getIds());
        return Result.success().data(Boolean.TRUE).build();
    }
}
